/**
 * IsClassDependency.java
 * created: 1 feb. 2011
 *
 * Copyright 2011 - present WPG Uitgevers
 * No warranties, use at your own risk.
 */
package com.googlecode.wcbunittests.test.matchers;

import org.hamcrest.Description;
import org.hamcrest.TypeSafeMatcher;

/**
 * This matcher asserts that a component's dependencies are all valid classes. This should work
 * if the classes defined are in the classpath of the test runner. This is to fix a flaw in GX's
 * dependency API, which blindly assumes that strings passed to it are actually fully qualified
 * classnames, whilst that is only tested at initialization and will result in errors at runtime.
 * <p>
 * Using this matcher will pull the error forward towards the build / test cycle.
 * <p>
 * A new or extended API that only accepts Class instances would pull the error forward even more,
 * causing typesafety exceptions if an invalid value was passed. If dependencies had a common
 * ancestor or interface, that API could be even tighter constrained to only accept dependency
 * types.
 * 
 * @since v. 1.0.0 1 feb. 2011
 * @author Freek Wielstra
 */
public class IsInstantialbleClassName extends TypeSafeMatcher<String>{
        
    public void describeTo(Description description) {
       description.appendText("An instantiable classname of a class on the classpath");
    }

    @Override
    public boolean matchesSafely(String className) {
        try {
            Class.forName(className);
            return true;
        } catch (ClassNotFoundException e) {
            return false;
        }
    }
}
